<template>
  <div class="container">
    <el-input
        type="textarea"
        class="textarea left"
        :row="row"
        v-model="leftText"
    />
    <el-input
        type="textarea"
        class="textarea right"
        :row="row"
        v-model="rightText"
    />
  </div>
  <el-footer>
    <el-form :inline="true">
      <el-form-item style="width:8%">
        <el-input v-model="separator" placeholder="分隔符"/>
      </el-form-item>
      <el-form-item style="width:25%">
        <el-input v-model="tableName" placeholder="表名"/>
      </el-form-item>
      <el-form-item lable="数据库类型" style="width:20%">
        <el-select v-model="dbType" placeholder="请选择数据库类型" filterable>
          <el-option v-for="item in dbOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getInsertForeach" type="primary">
          转换
        </el-button>
        <el-button @click="copy" type="primary">
          复制
        </el-button>
      </el-form-item>
    </el-form>
  </el-footer>
</template>
<script>
import {leftTextToStrArr, copy2Clipboard, getForeachStr, getForeachItemStr} from "../../utils/util.js";

export default {
  name: 'InsertBatch',
  props: {
    inForeachPrefix: String,
    outForeachPrefix: String,
    suffix: String,
    dbOptions: Array
  },
  data() {
    return {
      row: 10,
      leftText: '',
      rightText: '',
      separator: ',',
      tableName: '',
      dbType: 'mysql',
    }
  },
  methods: {
    copy() {
      copy2Clipboard(this)
    },
    getInsertForeach() {
      if (this.tableName === undefined || this.tableName === '' || this.tableName == null) {
        this.tableName = 'table_name'
      }
      const leftTextArr = leftTextToStrArr(this)
      const columns = leftTextArr.join(', ')
      const foreachStr = getForeachStr(this)
      switch (this.dbType) {
        case 'mysql':
        case 'postgresql':
          this.rightText = `insert into ${this.tableName} (${columns})\nvalues\n${foreachStr}`
          break;
        case 'oracle':
          this.rightText =
              `begin;\n<foreach collection="list" item="item" separator=";">\ninsert into ${this.tableName} (${columns})\nvalue\n(\n${getForeachItemStr(leftTextToStrArr(this))}\n)\n</foreach>\nend;\ncommit;`
          break;
        default:
          this.$message.error('当前类型暂未支持')
      }
    },
  }
}
</script>
<style></style>
